# Again this guy was originally found on the mod_python mailing list.

from mod_python import apache
from mod_python import util
import sys

# -----------------------------------------------------------------------------
# Handlers
# -----------------------------------------------------------------------------
def handler(req, sesh=None):
	""" need to debug a form submission... then point it at a dump handler.
	"""
	req.content_type = "text/plain"
	req.send_http_header()
	_dump(req,"Request", req.the_request)
	_dump(req,"Protocol", req.protocol)
	_dump(req,"Hostname", req.hostname)
	_dump(req,"Status line", req.status_line)
	_dump(req, "Method", req.method)
	_dump(req,"Content handler", req.handler)
	_dump(req, "Unparsed uri", req.unparsed_uri)
	_dump(req, "Uri", req.uri)
	_dump(req, "Filename", req.filename)
	_dump(req, "Path info", req.path_info)
	_dump(req, "Mtime", str(req.mtime))
	_dump(req, "Remaining", str(req.remaining))
	_dump(req, "Content type", req.content_type)
	_dump(req, "Parsed uri", str(req.parsed_uri))
	_dump(req, "finfo", str(req.finfo))
	_dumpdict(req, "headers_in",req.headers_in)
	_dumpdict(req, "headers_out", req.headers_out)
	_dumpdict(req, "err_headers_out", req.err_headers_out)
	_dumpdict(req, "subprocess_env", req.subprocess_env)
	_dumpdict(req, "notes", req.notes)
	_dumpdict(req, "form fields", util.FieldStorage(req, keep_blank_values=1))
	_dumpdict(req, "locals", locals())
	_dump(req, "pythonpath", str(sys.path))
	return apache.OK
	    
# -----------------------------------------------------------------------------
# Helpers
# -----------------------------------------------------------------------------
def _dumpdict(req,title, d):
	req.write("*"+title + "\n")
	for key in d.keys():
		req.write(str(key) + ": " + str(d[key]) + "\n")
		
# -----------------------------------------------------------------------------
def _dump(req,title,s):
	req.write(title+": "+s+"\n")

